<?php

namespace App\Http\Controllers\Admin\Contract;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Contract\ContractRepository;
use App\Rules\ContractValidate;
use Illuminate\Http\Request;

// 合同管理
class IndexController extends BaseController
{
    private $repoist;

    public function __construct(ContractRepository $repository)
    {
        $this->repoist = $repository;
    }


    public function index(Request $request)
    {
        $where = [];
        if ($request->has('number') && !empty($request->get('number'))) {
            $where['where']['number'] = $request->get('number');
        }
        if ($request->has('title') && !empty($request->get('title'))) {
            $where['like']['title'] = $request->get('title');
        }
        if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
            $where['where']['status'] = (int)$request->get('status');
        }
//        print_r($request);
        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
            $time = $request->get('dateRange');
            $where['between'] = ['over_at' => [$time]];
        }
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
//         var_dump($where);

        $dataArr = $this->repoist->unifiedIndex($where);
        if (is_array($dataArr)) {
            $dataArr['statusList'] = config('card.contractStatus');
        }
        return $dataArr;
    }


    public function edit(Request $request)
    {
        $Validator = new ContractValidate();
        if (!$Validator->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }
//        省市区域把数组变成字符保存
        $area = $request->get('area');
        if (is_array($area)) {
            $request->offsetSet('area', implode(',', $area));
        }
        $pics = $request->get('pics');
//        if (!empty($pics) && is_array($pics)) {
//            $request->offsetSet('pics', json_encode($pics));
//        }
//        不用pics了
        $request->offsetSet('pics', json_encode([]));
        if (isset($request['deposit_at']) && empty($request['deposit_at'])) {
            unset($request['deposit_at']);
        }
        if ($request->has('id')) {
            return $this->repoist->unifiedEdit($request, [], 'number');
        } else {
//            print_r($request->all());
            return $this->repoist->unifiedAdd($request, [], 'number');
        }

    }

    public function add(Request $request)
    {
        $request->offsetSet('users_id', $request->user->id);
        return $this->edit($request);

    }

    public function del(Request $request, $id)
    {
        return $this->repoist->unifiedDel($id);
    }
//    public function changeStatus(Request $request)
//    {
//        $id = $request->get("id");
//        // 需要修改两个地方 一个是状态 一个 app是否上架
//        $status = (int)$request->get('status');
//        return $this->repoist->unifiedUpdateStatus($id, $status);
//    }
}
